package com.example.button;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button mBtn2, mBtn3, mBtn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtn2 = findViewById(R.id.btn_2);
        mBtn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"你点击了第二个按钮",Toast.LENGTH_SHORT).show();
            }
        });

        mBtn3 = findViewById(R.id.btn_3);
        mBtn4 = findViewById(R.id.btn_4);
        OnClick onclick = new OnClick();
        mBtn3.setOnClickListener(onclick);
        mBtn4.setOnClickListener(onclick);

    }
    public void MakeToast(View view) {
        Toast.makeText(this,"你点击了第一个按钮",Toast.LENGTH_SHORT).show();
    }

    private class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_3:
                    Toast.makeText(MainActivity.this,"你点击了第三个按钮",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn_4:
                    Toast.makeText(MainActivity.this,"你点击了第四个按钮",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }
}